/**
 * Course Project for User Interface Programming I, 2011
 * Group 19
 */
package justdoit.view;

import java.awt.Color;
import java.awt.Point;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.basic.BasicListUI.ListSelectionHandler;
import justdoit.actions.DeleteTaskAction;
import justdoit.actions.EditTaskAction;
import justdoit.actions.MarkAsDoneAction;
import justdoit.model.Task;
import justdoit.view.model.TaskListModel;

/**
 * The List to contain most important information of tasks
 * @author Can
 */
public class TaskList extends JList implements DragSourceListener,
        DragGestureListener {

    private DragSource ds;
    private StringSelection transferable;
    private JPopupMenu popMenu;

    public TaskList(TaskListModel model) {
        super(model);
        // TODO: the color should be get from somewhere
        this.setBackground(Color.WHITE);
        //this.setDragEnabled(true);
        //this.setTransferHandler(new ListTransferHandler());
        this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ds = new DragSource();
        DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(this,
                DnDConstants.ACTION_MOVE, this);

        popMenu = new JPopupMenu();
        popMenu.add(new JMenuItem(new EditTaskAction()));
        popMenu.add(new JMenuItem(new DeleteTaskAction()));
        popMenu.addSeparator();
        popMenu.add(new JMenuItem(new MarkAsDoneAction()));

        this.setFocusable(false);
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // if double clicks
                if (e.getComponent().isEnabled() && e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                    Point p = e.getPoint();
                    int index = locationToIndex(p);
                    if (index != -1) {
                        Task task = ((TaskListModel) getModel()).getTask(index);
                        if (task == null) {
                            System.out.println("Should not happen!");
                            return;
                        }
                        EditTaskDialog etd = new EditTaskDialog(task);
                        etd.setVisible(true);
                    }
                } else if (e.getButton() == MouseEvent.BUTTON3) {
                    // right click
                    popMenu.show(e.getComponent(), e.getX(), e.getY());
                    popMenu.setVisible(true);
                } else {
                    popMenu.setVisible(false);
                }
            }
        });

        this.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                TaskListModel model = (TaskListModel) getModel();
                int selectedIndex = e.getFirstIndex();
                Task task = model.getTask(selectedIndex);
                if (task != null) {
                    String separator = System.getProperty("line.separator");
                    separator = ".  ";
                    String message = "Priotity: " + task.getDisplayPriority() + separator
                            + "Progress: " + task.getProgress() + "%" + separator
                            + "Category: " + task.getCategory() + separator
                            + "Deadline: " + task.getDeadLine() + separator
                            + "Reminder: " + task.getReminder() + separator;
                    setToolTipText(message);
                }
            }
        });

    }

    public void dragGestureRecognized(DragGestureEvent dge) {
        System.out.println("Drag Gesture Recognized!");
        transferable = new StringSelection(this.getSelectedValue().toString());
        ds.startDrag(dge, DragSource.DefaultMoveDrop, transferable, this);
    }

    public void dragEnter(DragSourceDragEvent dsde) {
        System.out.println("Drag Enter");
    }

    public void dragExit(DragSourceEvent dse) {
        System.out.println("Drag Exit");
    }

    public void dragOver(DragSourceDragEvent dsde) {
        System.out.println("Drag Over");
    }

    public void dragDropEnd(DragSourceDropEvent dsde) {
        System.out.print("Drag Drop End: ");
        if (dsde.getDropSuccess()) {
            System.out.println("Succeeded");
        } else {
            System.out.println("Failed");
        }
        System.out.println(((TaskList) dsde.getDragSourceContext().getComponent()).getSelectedValue());
    }

    public void dropActionChanged(DragSourceDragEvent dsde) {
        System.out.println("Drop Action Changed");
    }
}
